package com.rt.tmpt.others.pageUtils;

import com.googlecode.genericdao.search.Search;
import com.rt.tmpt.dto.PaginationDto;

import java.io.IOException;
import java.util.*;

/**
 * Pagination service
 * @author wxy
 *
 * @param <T>
 */
@SuppressWarnings("unchecked")
public class PaginationService<T>{
	private static int pagesLimit = 10;
	private static int pageSizeLimit = 200;
	private static List<Integer> pageSizeList = new ArrayList<Integer>();
	private static String pageSizeIndexDefault;
	private static int pageSizeDefault = 10;
	
	static{
		try {
			Properties prop = new Properties();
			prop.load(PaginationService.class.getResourceAsStream("pagination.properties"));
			pagesLimit = Integer.valueOf(prop.get("pagesLimit").toString());
			pageSizeLimit = Integer.valueOf(prop.get("pageSizeLimit").toString());

			//页面显示的每页记录数列表
			Iterator it = prop.keySet().iterator();
			while(it.hasNext()){
				String key = (String)it.next();
				if(key.indexOf("pageSizeList")==0){
					pageSizeList.add(
							Integer.valueOf(prop.get(key).toString()));
				}
			}
			//
			pageSizeIndexDefault = prop.get("pageSizeIndexDefault").toString();
			pageSizeDefault = Integer.valueOf(prop.get(pageSizeIndexDefault).toString());
		
		} catch (IOException e) {
			pageSizeList.add(40);
			pageSizeList.add(60);
			pageSizeList.add(80);
			pageSizeList.add(100);
			e.printStackTrace();
		}
		
		Comparator<Integer> comparator = new Comparator<Integer>() {
			public int compare(Integer o1, Integer o2) {
				return o1-o2;
				//return o1-o2; order by desc
			}
			
		};
		
		Collections.sort(pageSizeList,comparator);
	}
	

	private PaginationDto paginationDto = new PaginationDto();
	private Search search;
	
	public PaginationService(Search search){
		this.search = search;
	}
	
	/**
	 * 验证提交的每页记录数不要超过一定值
	 * @return
	 */
	public boolean validataPageSize(){
		//pageSizeDefault
		if(search.getMaxResults()==0){
			search.setMaxResults(pageSizeDefault);
		}
		
		int pageSizeRequest = search.getMaxResults();
		if(pageSizeRequest>pageSizeLimit){
			return false;
		}
		return true;
	}
	
	/**
	 * 加载分页DTO
	 * @param recordList
	 * @param totalCount
	 */
	public void assemblePaginationDto(List<T> recordList, int totalCount){
		int curPageNo = search.getPage()+1;
		int pageSize = search.getMaxResults();
		paginationDto.setRecordList(recordList);
		paginationDto.setPageNo(curPageNo);
		paginationDto.setPageSize(pageSize);
		paginationDto.setTotalRecordCount(totalCount);
		
		if(totalCount==0 || pageSize==0){
			paginationDto.setTotalPageCount(1);
		}else if(totalCount%pageSize==0){
			paginationDto.setTotalPageCount(totalCount/pageSize);
		}else{
			paginationDto.setTotalPageCount(totalCount/pageSize+1);
		}
		
		if(curPageNo>1){
			paginationDto.setHasPreviousPage(true);
			paginationDto.setPreviousPageNo(paginationDto.getPageNo()-1);
		}
		
		if(curPageNo*pageSize<totalCount){
			paginationDto.setHasNextPage(true);
			paginationDto.setNextPageNo(paginationDto.getPageNo()+1);
		}
		
		if(paginationDto.getPageNo() < paginationDto.getTotalPageCount()){
			paginationDto.setHasEndPage(true);
			paginationDto.setEndPageNo(paginationDto.getTotalPageCount());
		}
		
		//计算页面显示的分页列表
		int totalPageCount = paginationDto.getTotalPageCount();
		if(totalPageCount==0){
			paginationDto.getPageList().add(1);
		}
		else if(totalPageCount<=pagesLimit){
			for(int i=1;i<=totalPageCount;i++){
				paginationDto.getPageList().add(i);
			}
		}else{ 
			if(curPageNo<=pagesLimit/2){
				for(int i=1;i<=pagesLimit;i++){
					paginationDto.getPageList().add(i);
				}
			}
			else if(curPageNo>totalPageCount-pagesLimit/2){
				for(int i=pagesLimit-1;i>=0;i--){
					paginationDto.getPageList().add(totalPageCount-i);
				}
			}
			else{
				for(int i=0;i<pagesLimit;i++){
					int pageNoTmp = curPageNo-pagesLimit/2+i+1;
					if(pageNoTmp>totalPageCount){break;}
					paginationDto.getPageList().add(pageNoTmp);
				}
			}
			
		}
		
		//
		paginationDto.setPageSizeList(pageSizeList);
		
	}
	
	@Override
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append("---------------").append("\r\n");
		sb.append("totalPageCount:"+paginationDto.getTotalPageCount()).append("\r\n");
		sb.append("pageNo:"+paginationDto.getPageNo()).append("\r\n");
		sb.append("pageSize:"+paginationDto.getPageSize()).append("\r\n");
		sb.append("totalRecordCount:"+paginationDto.getTotalRecordCount()).append("\r\n");
		sb.append("hasPreviousPage:"+paginationDto.isHasPreviousPage()).append("\r\n");
		sb.append("hasNextPage:"+paginationDto.isHasNextPage()).append("\r\n");
		sb.append("recordList:"+paginationDto.getRecordList()).append("\r\n");
		sb.append("recordList size:"+paginationDto.getRecordList().size()).append("\r\n");
		sb.append("previousPageNo:"+paginationDto.getPreviousPageNo()).append("\r\n");
		sb.append("nextPageNo:"+paginationDto.getNextPageNo()).append("\r\n");
		sb.append("this.hasEndPage："+paginationDto.isHasEndPage()).append("\r\n");
		sb.append("this.endPageNo："+paginationDto.getEndPageNo()).append("\r\n");
		return sb.toString();
	}

	public PaginationDto<T> getPaginationDto() {
		return paginationDto;
	}

	public void setPaginationDto(PaginationDto<T> paginationDto) {
		this.paginationDto = paginationDto;
	}

	
}
